package com.clouddisk.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.clouddisk.common.annotation.Log;
import com.clouddisk.common.core.controller.BaseController;
import com.clouddisk.common.core.domain.R;
import com.clouddisk.common.core.domain.dto.file.DeleteRecoveryFileDTO;
import com.clouddisk.common.core.domain.dto.recoveryfile.BatchDeleteRecoveryFileDTO;
import com.clouddisk.common.core.domain.dto.recoveryfile.RestoreFileDTO;
import com.clouddisk.common.core.domain.entity.RecoveryFile;
import com.clouddisk.common.enums.BusinessType;
import com.clouddisk.system.component.AsyncTaskComp;
import com.clouddisk.system.domain.vo.file.RecoveryFileListVo;
import com.clouddisk.system.service.IRecoveryFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZJH
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/recoveryfile")
public class SysRecoveryFileController extends BaseController {

    @Resource
    IRecoveryFileService recoveryFileService;
    @Resource
    AsyncTaskComp asyncTaskComp;

    @Log(title = "删除回收文件", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/deleterecoveryfile", method = RequestMethod.POST)
    @ResponseBody
    public R<String> deleteRecoveryFile(@RequestBody DeleteRecoveryFileDTO deleteRecoveryFileDTO) {
        RecoveryFile recoveryFile = recoveryFileService.getById(deleteRecoveryFileDTO.getRecoveryFileId());
        asyncTaskComp.deleteUserFile(recoveryFile.getUserFileId());
        recoveryFileService.removeById(deleteRecoveryFileDTO.getRecoveryFileId());
        return R.ok("删除成功");
    }

    @Log(title = "批量删除回收文件", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/batchdelete", method = RequestMethod.POST)
    @ResponseBody
    public R<String> batchDeleteRecoveryFile(@RequestBody BatchDeleteRecoveryFileDTO batchDeleteRecoveryFileDTO) {
        List<RecoveryFile> recoveryFileList = JSON.parseArray(batchDeleteRecoveryFileDTO.getRecoveryFileIds(), RecoveryFile.class);
        for (RecoveryFile recoveryFile : recoveryFileList) {
            RecoveryFile recoveryFile1 = recoveryFileService.getById(recoveryFile.getRecoveryFileId());

            if (recoveryFile1 != null) {
                asyncTaskComp.deleteUserFile(recoveryFile1.getUserFileId());
                recoveryFileService.removeById(recoveryFile1.getRecoveryFileId());
            }

        }
        return R.ok("批量删除成功");
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public R<List<RecoveryFileListVo>> getRecoveryFileList(@RequestBody DeleteRecoveryFileDTO deleteRecoveryFileDTO) {
        List<RecoveryFileListVo> recoveryFileList = recoveryFileService.selectRecoveryFileList(getUserId(),deleteRecoveryFileDTO.fileDeptName);

        return R.ok(recoveryFileList);
    }

    @Log(title = "还原文件", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/restorefile", method = RequestMethod.POST)
    @ResponseBody
    public R<String> restoreFile(@RequestBody RestoreFileDTO restoreFileDto) {
        recoveryFileService.restorefile(restoreFileDto.getDeleteBatchNum(), restoreFileDto.getFilePath(), getUserId(),restoreFileDto.getFileDeptName());
        return R.ok("还原成功！");
    }

}
